package com.cheer.ba.view.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cheer.ba.R;
import com.cheer.ba.constant.IntentCode;
import com.cheer.ba.listener.OnItemClickListener;
import com.cheer.ba.model.LookMyBillModel;
import com.cheer.ba.utils.Currency;
import com.cheer.ba.utils.StringUtils;
import com.cheer.ba.utils.TextViewUtils;
import com.cheer.lib_recycle_swipe.SwipeMenuAdapter;

import java.util.List;

/**
 * 我的账单
 * Created by Hq on 2017/7/13.
 */
public class LookMyBillAdp extends SwipeMenuAdapter<LookMyBillAdp.DefaultViewHolder> {

    //可滑动
    public static final int VIEW_TYPE_MENU = 1;
    //不可滑动
    public static final int VIEW_TYPE_NONE = 2;

    private List<LookMyBillModel> mBills;

    private int page_from;

    private OnItemClickListener mOnItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }

    public LookMyBillAdp(List<LookMyBillModel> mBills, int page_from) {
        this.mBills = mBills;
        this.page_from = page_from;
    }

    @Override
    public int getItemViewType(int position) {
        if (page_from != IntentCode.General.JUMP_CLOUD) {
            //云记账
            return VIEW_TYPE_NONE;
        } else {
            LookMyBillModel model = getItem(position);
            //1个人 2企业
            int accountType = model.accountType;
            //1 进账 2出账
            int logType = model.logType;
            if (StringUtils.isEmpty(model.month)) {
                if (accountType == 1) {
                    return VIEW_TYPE_MENU;
                } else {
                    return VIEW_TYPE_NONE;
                }
            } else {
                return VIEW_TYPE_NONE;
            }
        }
    }

    @Override
    public int getItemCount() {
        return mBills == null ? 0 : mBills.size();
    }

    public LookMyBillModel getItem(int position) {
        if (null != mBills && !mBills.isEmpty()) {
            return mBills.get(position);
        }
        return new LookMyBillModel();
    }

    @Override
    public View onCreateContentView(ViewGroup parent, int viewType) {
        return LayoutInflater.from(parent.getContext()).inflate(R.layout.adp_bill_book, parent, false);
    }

    @Override
    public DefaultViewHolder onCompatCreateViewHolder(View realContentView, int viewType) {
        return new DefaultViewHolder(realContentView);
    }

    @Override
    public void onBindViewHolder(DefaultViewHolder holder, int position) {
        LookMyBillModel model = getItem(position);
        if (null != model) {
            String createTime = model.createTime;
            if (!StringUtils.isEmpty(model.month)) {
                holder.mTopRela.setVisibility(View.VISIBLE);
                holder.mLinearContext.setVisibility(View.GONE);
                if (StringUtils.isToyear(createTime)) {
                    holder.mTxtMonth.setText(model.month + "月");
                } else {
                    holder.mTxtMonth.setText(StringUtils.getDetailStr1(createTime));
                }
            } else {
                holder.mTopRela.setVisibility(View.GONE);
                holder.mLinearContext.setVisibility(View.VISIBLE);
                if (StringUtils.isToday(createTime)) {
                    holder.mTxtDay.setText("今天");
                } else if (StringUtils.isYesterday(createTime)) {
                    holder.mTxtDay.setText("昨天");
                } else {
                    holder.mTxtDay.setText(StringUtils.getWeek(StringUtils.stampToDate(createTime, "yyyy-MM-dd")));
                }
                holder.mTxtTime.setText(StringUtils.getDetailStr1(createTime));
                String money = Currency.returnDollar(model.money);
                int accountType = model.accountType;
                int logType = model.logType;
                if (logType == 1) {
                    //进账
                    money = "+" + money;
                    holder.mImgIcon.setBackgroundResource(R.drawable.ic_incream);
                    if (page_from == IntentCode.General.JUMP_MONEY) {
                        //提现
                        String bankNum = model.bankNo;
                        if (!StringUtils.isEmpty(bankNum)) {
                            holder.mTxtDesc.setText("银行卡尾号:" + bankNum.substring((bankNum.length() - 4), bankNum.length()));
                        }
                    } else {
                        if (accountType == 1) {
                            holder.mTxtDesc.setText("个人支入");
                        } else if (accountType == 2) {
                            holder.mTxtDesc.setText("企业支入");
                        } else {
                            holder.mTxtDesc.setText("支入");
                        }
                    }
                    TextViewUtils.setSpannableStyleColor(money, 0, money.length() - 1, holder.mTxtMoney, R.color.color_main);
                } else {
                    //出账
                    money = "-" + money;
                    holder.mImgIcon.setBackgroundResource(R.drawable.ic_outcream);
                    if (page_from == IntentCode.General.JUMP_MONEY) {
                        //提现
                        String bankNum = model.bankNo;
                        if (!StringUtils.isEmpty(bankNum)) {
                            holder.mTxtDesc.setText("银行卡尾号:" + bankNum.substring((bankNum.length() - 4), bankNum.length()));
                        }
                    } else {
                        if (accountType == 1) {
                            holder.mTxtDesc.setText("个人支出");
                        } else if (accountType == 2) {
                            holder.mTxtDesc.setText("企业支出");
                        } else {
                            holder.mTxtDesc.setText("支出");
                        }
                    }
                    TextViewUtils.setSpannableStyleColor(money, 0, money.length() - 1, holder.mTxtMoney, R.color.color_333333);
                }
                if (page_from == IntentCode.General.JUMP_MONEY) {
                    if (StringUtils.isEmpty(model.auditRemark)) {
                        holder.mTxtRemark.setVisibility(View.GONE);
                    } else {
                        holder.mTxtRemark.setVisibility(View.VISIBLE);
                        holder.mTxtRemark.setText(model.auditRemark);
                    }
                    holder.mTxtStatus.setVisibility(View.VISIBLE);
                    if (model.auditStatus == 1) {
                        //申请中
                        holder.mTxtStatus.setText("申请中");
                    } else if (model.auditStatus == 2) {
                        //驳回
                        holder.mTxtStatus.setText("驳回");
                    } else if (model.auditStatus == 3) {
                        holder.mTxtStatus.setText("提现成功");
                    }
                } else {
                    holder.mTxtStatus.setVisibility(View.GONE);
                    if (StringUtils.isEmpty(model.remark)) {
                        holder.mTxtRemark.setVisibility(View.GONE);
                    } else {
                        holder.mTxtRemark.setVisibility(View.VISIBLE);
                        holder.mTxtRemark.setText(model.remark);
                    }
                }
                holder.setOnItemClickListener(mOnItemClickListener);
            }
        }
    }

    static class DefaultViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        //是否展示月份
        RelativeLayout mTopRela;
        //月份
        TextView mTxtMonth;

        //内容区域
        LinearLayout mLinearContext;
        //本月的日期
        TextView mTxtDay;
        //本月日期下的时间
        TextView mTxtTime;
        //进出账的图标
        ImageView mImgIcon;
        //进出账文字描述
        TextView mTxtDesc;
        //进出账金额
        TextView mTxtMoney;
        //进出账备注
        TextView mTxtRemark;
        //审核状态
        TextView mTxtStatus;

        OnItemClickListener mOnItemClickListener;

        public DefaultViewHolder(View itemView) {
            super(itemView);
            itemView.setOnClickListener(this);
            mTopRela = (RelativeLayout) itemView.findViewById(R.id.id_rela_top);
            mTxtMonth = (TextView) itemView.findViewById(R.id.id_book_month_txt);

            mLinearContext = (LinearLayout) itemView.findViewById(R.id.id_linear_content);
            mTxtDay = (TextView) itemView.findViewById(R.id.id_book_day_txt);
            mTxtTime = (TextView) itemView.findViewById(R.id.id_book_hour_txt);
            mImgIcon = (ImageView) itemView.findViewById(R.id.id_book_incream_img);
            mTxtDesc = (TextView) itemView.findViewById(R.id.id_book_type_txt);
            mTxtMoney = (TextView) itemView.findViewById(R.id.id_book_money_txt);
            mTxtRemark = (TextView) itemView.findViewById(R.id.id_book_desc_txt);
            mTxtStatus = (TextView) itemView.findViewById(R.id.id_bill_status);
        }

        public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
            this.mOnItemClickListener = onItemClickListener;
        }

        @Override
        public void onClick(View v) {
            if (mOnItemClickListener != null) {
                mOnItemClickListener.onItemClick(getAdapterPosition());
            }
        }
    }
}
